package com.hbnu.exercise.thread;

public class Box
{
    private int product;

    public synchronized void AddProduct()
    {
        if (product > 20)
        {
            try
            {
                wait();
            } catch (InterruptedException e)
            {
                throw new RuntimeException(e);
            }
        }
        else
        {
            product++;
            System.out.println("生产者生产了" + product + "号牛奶");
            notifyAll();
        }
    }

    public synchronized void GetProduct()
    {
        if (product <= 0)
        {
            try
            {
                wait();
            } catch (InterruptedException e)
            {
                throw new RuntimeException(e);
            }
        }
        else
        {
            System.out.println("消费者消费了" + product + "号牛奶");
            product--;
            notifyAll();
        }
    }
}
